package com.gupt.youhd.config;/*
@author name
@data 2023/4/10-10:42
*/

import com.gupt.youhd.interceptor.CheckTokenInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.config.annotation.*;

@Slf4j
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {

    @Value("${youHD.path}")
    private String uploadPath;

    @Autowired
    private CheckTokenInterceptor checkTokenInterceptor;

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("");  //前端登录页面
    }

    /**
     * 配置拦截器
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        InterceptorRegistration interceptor = registry.addInterceptor(checkTokenInterceptor);

        interceptor.addPathPatterns("/**")
                .excludePathPatterns("/login")
                .excludePathPatterns("/img_youHD/**")
                .excludePathPatterns("/register")
                .excludePathPatterns("/getCaptcha")
                .excludePathPatterns("/getCaptcha/next")
                .excludePathPatterns("/updatePwd")
                .excludePathPatterns("/register/getCaptcha");
    }


    //配置资源真实路径
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/img_youHD/**")     //当请求upload/**时 指定资源真实位置:uploadPath
                .addResourceLocations("file:" + uploadPath);   //访问本地磁盘文件  所以路径前＋file：
    }

    @Bean
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
}
